var ue,imgFile;
$(function () {
    ue =new UE.ui.Editor({
        initialFrameHeight:300,initialFrameWidth:700
    });
    ue.render("editor");
    getSelectMess()
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#userState option:selected").text("全部");
    getUserList();
}
//获取课程信息
function getSelectMess(){
    JY.Model.loading();
    $.ajax({
        url : jypath + "/course/queryCourse",
        type : "post",
        data:{
            "pageNum":1,
            "pageSize":1000,
        },
        jsonType : "json",
        success : function(data) {
            console.log(data);
            if(data.obj.results!=null&&data.obj.results.length>0) {
                if (data.obj.results.length > 0) {
                    var html = "";
                    for (var i = 0; i < data.obj.results.length; i++) {
                        var l = data.obj.results[i];
                        html += "<option value='" + l.courseId + "'>" + l.courseName + "</option>"
                    }
                    $("#carousels").append(html);
                    $('#carousels').selectpicker('refresh');
                    $('#carousels').selectpicker('render');
                }
            }
            JY.Model.loadingClose();
            getUserList()
        }
    })

}
/*查询*/
function getUserList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
    var userState=$("#userState option:selected").val();
    var name=$("#courPageName").val().trim();
    var lecturer=$("#lecturer").val().trim();
    if($("#userState option:selected").text()=="全部"){
        userState="";
    }
	$.ajax({
		url : jypath + "/course/queryCoursePackage",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "state":userState,
            "coursePackageName":name,
            "lecturer":lecturer,
		},
		jsonType : "json",
		success : function(data) {
            var data=data instanceof Object ? data : JSON.parse(data);
            console.log(data)
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+l.coursePackageId+"'class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.coursePackageName+"</td>";
                    html+="<td class='center'>"+l.lecturer+"</td>";
                    var totalPrice=(l.totalPrice*0.01).toFixed(2);
                    html+="<td class='center'>"+totalPrice+"</td>";
                    var discountPrice=(l.discountPrice*0.01).toFixed(2);
                    html+="<td class='center'>"+discountPrice+"</td>";
                    html+="<td class='center'>"+l.pageckageTages+"</td>";
                    if(l.state==0){
                        html+="<td class='center'>失效</td>";
                    }else if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
//预览
function preview(obj) {
    var url= window.URL.createObjectURL(obj.files[0]);
    $(obj).prev().attr("src",url);
    imgFile = obj.files[0];
}
//新增
function addClick(){
    clearData();
    JY.Model.edit("auForm_edit","新增课程包",function(){
        var that=$(this);
        if(JY.Validate.form("generateForm")){
            var  carouselsArr= $('#carousels').val();
            if($(".preview_ing").attr("src")==""+jypath+"/static/images/back/add_img.jpg"){
                JY.Model.info("请先新增主题图片!");
            }else if(carouselsArr==null||carouselsArr.length==0){
                JY.Model.info("请先选择关联课程!");
            }else{
                $(".masks").removeClass("hide");
                var totalPrice=$("#generateForm input[name='totalPrice']").val();
                totalPrice=parseInt(totalPrice*100);
                var discountPrice=$("#generateForm input[name='discountPrice']").val();
                discountPrice=parseInt(discountPrice*100);
                $.ajax({
                    url : jypath + "/course/insertCoursePackage",
                    type : "post",
                    data:{
                        "coursePackageName":$("#generateForm input[name='coursePackageName']").val().trim(),
                        "pageckageTages":$("#generateForm input[name='pageckageTages']").val().trim(),
                        "lecturer":$("#generateForm input[name='lecturer']").val().trim(),
                        "totalPrice":totalPrice,
                        "discountPrice":discountPrice,
                        "state":$("#groomsSel").find("option:selected").val(),
                        "parameter":carouselsArr,
                        "courseDesc":UE.getEditor('editor').getContent(),
                    },
                    success : function(data) {
                        console.log(data)
                        console.log("新增")
                        if(data.res==1){
                            var relevanceId=data.obj;
                            var formData=new FormData();
                            formData.append("imageFile",imgFile);
                            formData.append("relevanceId",relevanceId);
                            formData.append("relevanceTpyeId",6);
                            formData.append("relevanceTpyeDetail",0);
                            $.ajax({
                                url: jypath + "/uploadVideoAndImage",
                                type: "post",
                                processData: false,
                                contentType: false,
                                data: formData,
                                success: function (data) {
                                    JY.Model.loadingClose();
                                    that.dialog("close");
                                    $(".masks").addClass("hide");
                                    JY.Model.info("操作成功!",function(){getUserList();});
                                }
                            })
                        }
                    }
                })
            }

        }
    });
}
//编辑
function editClick(){
    clearData();
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        //查询课包信息
        $.ajax({
            url : jypath + "/course/queryCoursePackage",
            type : "post",
            data:{
                "coursePackageId":chks[0],
                "type":1,
            },
            jsonType : "json",
            success : function(data) {
                console.log(data);
                var h=data.obj.results[0];
                $("#generateForm input[name='coursePackageName']").val(h.coursePackageName);
                $("#generateForm input[name='pageckageTages']").val(h.pageckageTages);
                $("#generateForm input[name='lecturer']").val(h.lecturer);
                var totalPrice=(h.totalPrice*0.01).toFixed(2);
                $("#generateForm input[name='totalPrice']").val(totalPrice);
                var discountPrice=(h.discountPrice*0.01).toFixed(2)
                $("#generateForm input[name='discountPrice']").val(discountPrice);
                $("#groomsSel").find("option[value = "+h.state+"]").prop("selected","selected");
                $(".preview_ing").attr("src",h.attachmentUrl);
                if(h.courseDesc!=null&&h.courseDesc!=undefined){
                    ue.setContent(h.courseDesc);
                }
                //查询课程信息
                $.ajax({
                    url : jypath + "/course/queryCoursePackageDetail",
                    type : "post",
                    data:{
                        "coursePackageId":chks[0]
                    },
                    jsonType : "json",
                    success : function(data) {
                        console.log(data);
                        console.log("获取课程信息")
                        var arrId=[];
                        $('.selectpicker').selectpicker('val', '').trigger("change");
                        if(data.obj.length>0&&data.obj!=null){
                            for(var k=0;k<data.obj.length;k++){
                                arrId.push(data.obj[k].courseId)
                            }
                        }else{
                            arrId=[];
                        }
                        $('.selectpicker').selectpicker('val', arrId).trigger("change");
                    }
                })
            }
        })
        JY.Model.message("auForm_edit","修改状态",function(){
            var that = $(this);
            var  carouselsArr= $('#carousels').val();
            var totalPrice=$("#generateForm input[name='totalPrice']").val();
            totalPrice=parseInt(totalPrice*100);
            var discountPrice=$("#generateForm input[name='discountPrice']").val();
            discountPrice=parseInt(discountPrice*100);
            if(imgFile.name!=undefined&&imgFile!=null&&imgFile!=""){
                console.log("修改了")
                $.ajax({
                    url : jypath + "/course/updateCoursePackage",
                    type : "post",
                    data:{
                        "coursePackageId":chks[0],
                        "coursePackageName":$("#generateForm input[name='coursePackageName']").val().trim(),
                        "pageckageTages":$("#generateForm input[name='pageckageTages']").val().trim(),
                        "lecturer":$("#generateForm input[name='lecturer']").val().trim(),
                        "totalPrice":totalPrice,
                        "discountPrice":discountPrice,
                        "state":$("#groomsSel").find("option:selected").val(),
                        "parameter":carouselsArr,
                        "courseDesc":UE.getEditor('editor').getContent(),
                    },
                    success : function(data) {
                        if(data.res==1){
                            var formData=new FormData();
                            if(imgFile.name!=undefined){
                                formData.append("imageFile",imgFile);
                                formData.append("relevanceTpyeDetail",0);
                            }
                            formData.append("relevanceId",chks[0]);
                            formData.append("relevanceTpyeId",6);
                            $.ajax({
                                url: jypath + "/uploadVideoAndImage",
                                type: "post",
                                processData: false,
                                contentType: false,
                                data: formData,
                                success: function (data) {
                                    that.dialog("close");
                                    JY.Model.info("操作成功!",function(){getUserList();});
                                }
                            })
                        }else{
                            that.dialog("close");
                        }
                    }
                })
            }else{
                console.log("没修改")
                $.ajax({
                    url : jypath + "/course/updateCoursePackage",
                    type : "post",
                    data:{
                        "coursePackageId":chks[0],
                        "coursePackageName":$("#generateForm input[name='coursePackageName']").val().trim(),
                        "pageckageTages":$("#generateForm input[name='pageckageTages']").val().trim(),
                        "lecturer":$("#generateForm input[name='lecturer']").val().trim(),
                        "totalPrice":totalPrice,
                        "discountPrice":discountPrice,
                        "state":$("#groomsSel").find("option:selected").val(),
                        "parameter":carouselsArr,
                        "path":"",
                        "courseDesc":UE.getEditor('editor').getContent(),
                    },
                    success : function(data) {
                        if(data.res==1){
                            that.dialog("close");
                            JY.Model.info("操作成功!",function(){getUserList();});
                        }
                    }
                })
            }

        });
    }else{
        JY.Model.info("您只能选择一条进行编辑!");
    }
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/course/updateCoursePackageState",
                type : "post",
                data:{
                    "state":states,
                    "parameter":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
function clearData(){
    $("#generateForm input").val("");
    $("#groomsSel").find("option[text = '否']").prop("selected","selected");
    $(".preview_ing").attr("src",""+jypath+"/static/images/back/add_img.jpg");
    UE.getEditor('editor').setContent('', false);
    $('.selectpicker').selectpicker('val', '').trigger("change");
    imgFile={};
    $("#input1").val("");
}
